﻿using System;
using System.Linq;
using System.Windows.Threading;
using System.Windows;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.Win32;

using TouchExplorer.Entities;

namespace TouchExplorer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            this.Closed += new EventHandler(MainWindow_Closed);
            SystemEvents.DisplaySettingsChanged += new EventHandler(SystemEvents_DisplaySettingsChanged);

            App.Current.OnBrowseFolder += new App.BrowseFolderHandler(MainWindow_OnBrowseFolder);
            App.Current.Clipboard = new ClipboardViewModel();
            Footer.DataContext = App.Current.Clipboard;

            var lst = new List<TouchDirectory>();

            lst.Add(new TouchDirectory(KnownFolders.Desktop as ShellObject));
            lst.Add(new TouchDirectory(KnownFolders.DocumentsLibrary as ShellObject));
            lst.Add(new TouchDirectory(KnownFolders.Downloads as ShellObject));
            lst.Add(new TouchDirectory(KnownFolders.MusicLibrary as ShellObject));
            lst.Add(new TouchDirectory(KnownFolders.PicturesLibrary as ShellObject));
            lst.Add(new TouchDirectory(KnownFolders.VideosLibrary as ShellObject));
            lst.Add(new TouchDirectory(KnownFolders.Computer as ShellObject));

            FileBrowserCtrl.Model.RootDirectories = lst;

            var commonPrograms = new TouchDirectory(KnownFolders.CommonPrograms as ShellObject);
            var programs = new TouchDirectory(KnownFolders.Programs as ShellObject);

            var lstDirectories = commonPrograms.GetDirectories().ToList();
            lstDirectories.AddRange(programs.GetDirectories());
            lstDirectories = lstDirectories.OrderBy(x => x.Name).ToList();

            var lstFiles = commonPrograms.GetFiles().ToList();
            lstFiles.AddRange(programs.GetFiles());
            lstFiles = lstFiles.OrderBy(x => x.Name).ToList();

            ProgramsBrowserCtrl.Model.RootDirectories = lstDirectories;
            ProgramsBrowserCtrl.Model.RootFiles = lstFiles;
        }

        void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
        {
            Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() =>
                {
                    this.InvalidateMeasure();
                    this.InvalidateArrange();
                    this.UpdateLayout();

                    Footer.InvalidateMeasure();
                    Footer.InvalidateArrange();
                    Footer.UpdateLayout();
                }));
        }

        void MainWindow_OnBrowseFolder(string folderPath)
        {
            if (Pivot.Items[Pivot.SelectedIndex] != BrowseFilesItem)
            {
                Pivot.SelectedIndex = Pivot.Items.IndexOf(BrowseFilesItem);
            }
            FileBrowserCtrl.SelectFolder(folderPath);
        }

        void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            App.LogError(e.ExceptionObject as Exception);
        }

        void MainWindow_Closed(object sender, EventArgs e)
        {
            var elt = (DataContext as IDisposable);
            if (elt != null) elt.Dispose();
        }

        private void CloseApp(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("You really want to close TouchExplorer ?", "Confirm", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                Application.Current.Shutdown();
            }
        }

        private void StartFlip(object sender, RoutedEventArgs e)
        {
            try
            {
                DwmpStartOrStopFlip3D(true);
            }
            catch (Exception)
            {
            }
        }

        [DllImport("dwmapi.dll", EntryPoint = "#105")]
        public static extern int DwmpStartOrStopFlip3D(bool fStart);
    }
}
